package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAdmStoreAuthor;
import com.zb.entity.dto.StoreAuthorDto;
import com.zb.entity.dto.StoreAuthorList;
import com.zb.entity.dto.StoreAuthorSelect;
import com.zb.mapper.ZjAdmStoreAuthorMapper;
import com.zb.service.ZjAdmStoreAuthorService;
import com.zb.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 后台仓库权限表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-14
 */
@Service
public class ZjAdmStoreAuthorServiceImpl extends ServiceImpl<ZjAdmStoreAuthorMapper, ZjAdmStoreAuthor> implements ZjAdmStoreAuthorService {
    @Autowired
    private ZjAdmStoreAuthorMapper admStoreAuthorMapper;

    // 新增或修改权限类型
    @Override
    public void save(StoreAuthorDto authorDto) {
        Assert.isTrue(Objects.equals(1, authorDto.getType()) || Objects.equals(2, authorDto.getType()) || Objects.equals(3, authorDto.getType()),
                "仓库类型不合法", authorDto.getType());
        Assert.isTrue(Objects.equals(1, authorDto.getCategory()) || Objects.equals(2, authorDto.getCategory()) || Objects.equals(3, authorDto.getCategory()) || Objects.equals(4, authorDto.getCategory()),
                "仓库类型不合法", authorDto.getType());
        Assert.isTrue(null == admStoreAuthorMapper.existByTypeAndCategory(authorDto.getId(), authorDto.getType(), authorDto.getCategory()),
                "权限类型已经存在，不可重复添加或更改！");

        ZjAdmStoreAuthor author = new ZjAdmStoreAuthor();
        author.setType(authorDto.getType());
        author.setCategory(authorDto.getCategory());
        if (null != authorDto.getId()) {
            this.updateById(author);
            return;
        }
        author.setId(IdWorker.getId());
        author.setDisabled(false);
        this.save(author);
    }

    // 仓库权限列表查询
    @Override
    public Map<String, Object> getList(StoreAuthorSelect authorSelect) {
        QueryWrapper<ZjAdmStoreAuthor> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_update");
        if (null != authorSelect.getType()) {
            Assert.isTrue(Objects.equals(1, authorSelect.getType()) || Objects.equals(2, authorSelect.getType()) || Objects.equals(3, authorSelect.getType()),
                    "仓库类型不合法", authorSelect.getType());
            wrapper.eq("type", authorSelect.getType());
        }
        if (null != authorSelect.getCategory()) {
            Assert.isTrue(Objects.equals(1, authorSelect.getCategory()) || Objects.equals(2, authorSelect.getCategory()) || Objects.equals(3, authorSelect.getCategory()) || Objects.equals(4, authorSelect.getCategory()),
                    "仓库类型不合法", authorSelect.getType());
            wrapper.eq("category", authorSelect.getType());
        }
        PageUtil.setPageLimit(authorSelect);
        Page<ZjAdmStoreAuthor> page = this.page(new Page<>(authorSelect.getPage(), authorSelect.getLimit()), wrapper);
        List<StoreAuthorList> authorLists = new ArrayList<>();
        page.getRecords().forEach(t -> authorLists.add(StoreAuthorList.beanToDto(t)));

        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), authorLists);
    }

    // 批量禁用或启用仓库权限
    @Override
    public void updateBatchDisableById(List<Long> idList, boolean disable) {
        admStoreAuthorMapper.updateBatchDisableById(idList, disable);
    }
}
